<template>
  <el-dialog :visible.sync="isShow" :before-close="handleCancelModal" :close-on-click-modal="false" width="640px">
    <span slot="title" v-text="formParams['group_id'] ? '编辑分组' : '创建分组'" />
    <!-- 数据表单 -->
    <el-form ref="formParams" :model="formParams" label-width="120px" size="small" style="padding-right: 40px">
      <div class="partition-container">
        <div class="partition-left">
          <el-form-item class="is-required" prop="group_name" label="分组名称：">
            <el-input v-model="formParams.group_name" placeholder="请输入分组名称" />
          </el-form-item>
          <el-form-item class="resize-none" prop="description" label="用途描述：">
            <el-input v-model="formParams.description" type="textarea" :autosize="{ minRows: 4 }" placeholder="请简单描述下该分组的用途" />
          </el-form-item>
          <el-form-item prop="sort" label="数据排序：">
            <el-input v-model="formParams.sort" placeholder="输入从小到大的排序数值" />
          </el-form-item>
          <el-form-item prop="status" label="是否启用：">
            <el-radio-group v-model="formParams.status" size="mini">
              <el-radio-button :label="0"><span style="padding: 0 5px">否</span></el-radio-button>
              <el-radio-button :label="1"><span style="padding: 0 5px">是</span></el-radio-button>
            </el-radio-group>
          </el-form-item>
        </div>
        <div class="partition-right">
          <div class="title">授权访问菜单：</div>
          <div class="rule-main">
            <el-tree
              ref="menus"
              :data="option.menuTree"
              :props="{ children: 'children', label: 'menu_name' }"
              node-key="menu_id"
              empty-text="没有任何菜单数据"
              icon-class="el-icon-arrow-right"
              show-checkbox
            />
          </div>
        </div>
      </div>
    </el-form>
    <!-- 提交按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button size="small" :disabled="isSubmit" @click="handleCancelModal">取 消</el-button>
      <el-button type="primary" size="small" :loading="isSubmit" @click="handleSubmitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { filterChildrenIds } from '@/utils/tree'
import { save } from '@/api/group'

export default {
  props: {
    'isShow': { type: Boolean, default: true },
    'isUpdate': { type: Boolean, default: false },
    'itemData': { type: Object, default: null },
    'option': { type: Object, default: null }
  },
  data: function() {
    return {
      isSubmit: false,
      formParams: {
        group_id: null,
        group_name: null,
        description: null,
        sort: 50,
        status: 1
      }
    }
  },
  mounted: function() {
    if (this.itemData) {
      this.formParams = this.itemData
      // 筛选保留子级节点
      const menus = filterChildrenIds(this.itemData.menus.map(item => { return item.menu_id }), this.itemData.menus)
      // 设置选择节点数据集
      this.$nextTick(() => {
        this.$refs.menus.setCheckedKeys(menus)
      })
    }
  },
  methods: {
    handleSubmitForm() {
      this.isSubmit = true
      this.formParams.menus = this.$refs.menus.getHalfCheckedKeys().concat(this.$refs.menus.getCheckedKeys())
      save(this.formParams, this.formParams.group_id || null).then(function() {
        this.$emit('on-cancel-modal', true)
      }.bind(this)).catch(function() {
        this.isSubmit = false
      }.bind(this))
    },
    // 取消弹出框
    handleCancelModal(refresh = false) {
      if (!this.isSubmit) {
        this.$emit('on-cancel-modal', refresh)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.partition-right {
  width: 200px;
  height: 262px;
  .rule-main {
    height: 227px;
  }
}
</style>
